package com.baizhi.springboot.controller;

import com.baizhi.springboot.service.LogService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LogController {
    @Autowired
    private LogService logService;
    @RequestMapping("/selectLogByPage")
    @ResponseBody
    public Map selectLogByPage(Integer page,Integer limit,String content,String type) {
        return logService.selectLogByPage(page,limit,content,type);
    }
    @RequestMapping("/deleteLogMany")
    @ResponseBody
    public Map deleteLogMany(@RequestParam("ids[]")Integer[] ids) {
        Map map=new HashMap();
        try {
            logService.deleteLogMany(ids);
            map.put("isOk", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isOk", false);
        }
        return map;
    }
    @RequestMapping("/daochuAllLog")
    public void daochuAllLog(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = logService.daochuAllLog();
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("log.xls","utf-8"));
        workbook.write(response.getOutputStream());
    }
}
